package com.edu.oracle.dao;

import com.edu.oracle.pojo.Course;
import com.edu.oracle.pojo.Teacher;
import com.edu.oracle.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TeacherDao {
    public List<Teacher> list() {

        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        try {
            //第一步和第二步jdbc操作步骤
            connection = JdbcUtils.getConnection();

            //3.创建执行对象
            String sql="select * from teacher";
            preparedStatement = connection.prepareStatement(sql);
            resultSet= preparedStatement.executeQuery();

            //4.处理结果集
            //4.1集合存储
            List<Teacher> teachers = new ArrayList<>();
            while (resultSet.next()){
                Teacher teacher = new Teacher();//实体类按住列存储，单位较小
                teacher.setTno(resultSet.getString("tno"));
                teacher.setTname(resultSet.getString("tname"));

                teachers.add(teacher);//list集合按对象存储
                System.out.println(teacher.getTname());
            }
            //4.2返回结果集
            return teachers;


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.关闭资源
            JdbcUtils.closeResoure(connection,preparedStatement,resultSet);
        }
        return null;
    }
}
